﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.SharePoint.Explorer;
using Microsoft.VisualStudio.SharePoint;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Shell;
using ServerExplorer.SharePointConnections.SharepointSiteInfo.Entities;

namespace ServerExplorer.SharePointConnections.SharepointSiteInfo
{
    // Enables Visual Studio to discover and load this extension.
    [Export(typeof(IExplorerNodeTypeExtension))]

    // Indicates that this class extends SharePoint site nodes in Server Explorer.
    [ExplorerNodeType(ExplorerNodeTypes.SiteNode)]

    public class SharepointSnifferSiteNodeExtension : IExplorerNodeTypeExtension
    {
        public void Initialize(IExplorerNodeType nodeType)
        {
            nodeType.NodeMenuItemsRequested += NodeMenuItemsRequested;
        }


        void NodeMenuItemsRequested(object sender, ExplorerNodeMenuItemsRequestedEventArgs e)
        {
            string title = "Get Info";
            IMenuItem menuItem = e.MenuItems.Add(title);
            menuItem.Click += menuItem_Click;
        }


        void menuItem_Click(object sender, MenuItemEventArgs e)
        {
            if (LoadPackage())
            {
                if (((IExplorerNode)e.Owner).Context != null)
                {
                    Access.conn = ((IExplorerNode)e.Owner).Context.SharePointConnection;
                    Access.fsh();
                }
                else
                {
                    System.Windows.MessageBox.Show("Conexão ainda não estabelicida. Abra a conexeção");
                }
            }
            else
            {
                System.Windows.MessageBox.Show("Pacote não pode ser carregado");
            }
        }

        private Boolean LoadPackage()
        {
            IVsPackage pkg = null;
            IVsShell shell = Package.GetGlobalService(typeof(SVsShell)) as IVsShell;
            if (shell != null)
            {
                Guid gpkg = new Guid("4fa861f1-18a9-47d4-a108-25d76309ff41");
                shell.LoadPackage(ref gpkg, out pkg);
            }
            return pkg != null;
        }

    }
}
